#!/usr/bin/env python

import pygame, sys, glob
from pygame import * #pode ser mudado para poupar memoria

import threading
import socket

import pygame, sys, glob
from pygame import * #pode ser mudado para poupar memoria

players = {}
action = ''


def Server():
    HOST = ''                 # Symbolic name meaning all available interfaces
    PORT = 50007              # Arbitrary non-privileged port
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((HOST, PORT))
    while 1:
        print("waiting for commands")
        global action, players
        data, id = s.recvfrom(2048)
        print("oops there we go")
        if not data: break
        action = data.decode("utf-8")
        if action == 'UP':
            players[id[0]].keydown(K_UP)
        elif action == 'LEFT':
            players[id[0]].keydown(K_LEFT)
        elif action == 'RIGHT':
            players[id[0]].keydown(K_RIGHT)
        elif action == 'A':
            players[id[0]].keydown(K_1)
        elif action == 'B':
            players[id[0]].keydown(K_2)
        elif action == 'C':
            players[id[0]].keydown(K_3)
        elif action == 'SIGN':
            if player not in players:
                players[id[0]] = player()
        elif action == 'LOGOUT':
            del players[id[0]]


h=250
w=550
#pygame.display.set_mode(resolution=(0,0), flags=0, depth=0)
screen = pygame.display.set_mode((w,h), pygame.FULLSCREEN)
clock = pygame.time.Clock()

ani_blast_r = glob.glob("sprites/energy r*.png")
ani_blast_l = glob.glob("sprites/energy l*.png")
ani_boom_r = glob.glob("sprites/boom r*.png")
ani_boom_l = glob.glob("sprites/boom l*.png")

class blast:
	def getTravelDist(self):
		return abs(self.x - self.x_init)
	def __init__(self,x,y,a):
		self.x_init = x
		self.x = x
		self.y = y
		self.a = a
		self.ani_speed_init=10
		self.ani_speed = self.ani_speed_init
		self.collided = 0

		if a > 0:
			self.ani = ani_blast_r
		else:
			self.ani = ani_blast_l
		self.ani.sort()
		self.ani_pos = 0

		self.img = pygame.image.load(self.ani[0])
		self.update()

	def update(self):
		self.x += self.a
		self.ani_speed -= 1

		if self.ani_speed == 0:
			self.img = pygame.image.load(self.ani[self.ani_pos])
			self.ani_speed = self.ani_speed_init
			if self.ani_pos == len(self.ani) -1:
				self.ani_pos = 0
				self.collided = 1
			else:
				self.ani_pos += 1
					
		screen.blit(self.img,(self.x-(self.img.get_width()/2),self.y))
		if self.a != 0:
			self.detectCollisions()

	def detectCollisions(self):
		for k in players.keys():
			if (self.x + self.img.get_width()) >= players[k].x and players[k].x >= self.x:
				if (self.y + self.img.get_height()) >= players[k].y and players[k].y >= self.y:
					players[k].dx = self.a*5
					if self.a > 0:
						self.ani = ani_boom_r
					else:
						self.ani = ani_boom_l
					self.ani_pos=0
					self.a=0


ani_r_jump = glob.glob("sprites/jump r*.png")
ani_r_jump.sort()
ani_l_jump = glob.glob("sprites/jump l*.png")
ani_l_jump.sort()
ani_r = glob.glob("sprites/walk r*.png")
ani_r.sort()
ani_l = glob.glob("sprites/walk l*.png")
ani_l.sort()
ani_kick_l = glob.glob("sprites/kick l*.png")
ani_kick_l.sort()
ani_kick_r = glob.glob("sprites/kick r*.png")
ani_kick_r.sort()
ani_punch_l = glob.glob("sprites/punch l*.png")
ani_punch_l.sort()
ani_punch_r = glob.glob("sprites/punch r*.png")
ani_punch_r.sort()
ani_cast_r = glob.glob("sprites/kamehameha r*.png")
ani_cast_r.sort()
ani_cast_l = glob.glob("sprites/kamehameha l*.png")
ani_cast_l.sort()

class player:
	def __init__(self):
		self.name = 0
		self.blast = 0
		self.blast_travel_dist = 2000
		self.casting = 0
		self.animating = 0
		self.yspeed = -3
		self.xspeed = 2
		self.direction = 0
		self.jumping = 0
		self.blast_dx = 0
		self.blast_x = 0
		self.blast_y = 0
		self.dx = 0
		self.dy = 0
		self.init_x = 150
		self.x = self.init_x
		self.init_y = 146
		self.y = self.init_y
		self.ani_speed_init=10
		self.ani_speed = self.ani_speed_init
		self.ani = ani_r
		self.ani_pos = 0
		self.img = pygame.image.load(self.ani[0])
		self.update()

	def keydown (self, key):
		if self.animating == 0:
			if key == K_RIGHT:
				self.ani = ani_r
				self.ani_pos=0
				self.dx = self.xspeed
				self.direction=key
			elif key == K_LEFT:
				self.ani = ani_l
				self.ani_pos=0
				self.dx= -self.xspeed
				self.direction=key
			elif key == K_UP:
				if self.jumping == 0:
					if self.direction == K_RIGHT:
						self.ani = ani_r_jump
					else:
						self.ani = ani_l_jump
					self.ani_pos=0
					self.dy = self.yspeed
					self.jumping = 1
			elif key == K_1:
				if self.direction == K_RIGHT:
					self.ani = ani_kick_r
				else:
					self.ani = ani_kick_l
				self.dx = 0
				self.ani_pos = 0
				self.animating = 1
			elif key == K_2:
				if self.direction == K_RIGHT:
					self.ani = ani_punch_r
					self.dx = 2
				else:
					self.ani = ani_punch_l
					self.dx = -2
				self.ani_pos = 0
				self.animating = 1
			elif key == K_3:
				if self.direction == K_RIGHT:
					self.ani = ani_cast_r
				else:
					self.ani = ani_cast_l
				self.ani_pos = 0
				self.casting = 1
				self.animating = 1
				self.dx = 0
	
	def update(self):
		
		if self.blast != 0:
			self.blast.update()
			if self.blast.getTravelDist() > self.blast_travel_dist:
				self.blast = 0
			if self.blast.collided == 1:
				self.blast = 0
		self.x += self.dx
		self.y += self.dy

		if self.jumping == 0:
			if self.x < 25 or (self.x > 255 and self.x < 290) or self.x > 525:#stickman cai
				self.dy = 5
		if self.y > self.init_y + 60: #stickman morre
			self.x = self.init_x
			self.y = self.init_y
			self.dy = 0
		self.ani_speed -= 1
		
		if self.jumping == 1:
			self.dy += 0.1
			if self.y >= self.init_y:
				self.jumping = 0
				self.y = self.init_y
				self.dy = 0
				self.ani_pos = 0
				self.ani_speed = self.ani_speed_init
		
		if self.animating == 1:
			self.detectCollisions()
		
		if self.ani_speed == 0:
			self.img = pygame.image.load(self.ani[self.ani_pos])
			self.ani_speed = self.ani_speed_init
			if self.ani_pos == len(self.ani) -1:
				#animacao acaba
				if self.casting == 1:
					if self.direction == K_RIGHT:
						self.blast = blast(self.x+60,self.y,3)
					else:
						self.blast = blast(self.x-60,self.y,-3)
					self.casting = 0
				if self.direction == K_RIGHT:
					self.ani = ani_r
				else:
					self.ani = ani_l
				self.ani_pos = len(self.ani) -1
				self.dx = 0
				self.animating = 0
			else:
				self.ani_pos += 1
		
		screen.blit(self.img,(self.x-(self.img.get_width()/2),self.y))

	def detectCollisions(self):
		for k in players.keys():
			if(players[k].x != self.x) or (players[k].y != self.y):
				if (self.x + self.img.get_width()) >= players[k].x and players[k].x >= self.x:
					if (self.y + self.img.get_height()) >= players[k].y and players[k].y >= self.y:
						if self.direction == K_LEFT:
							players[k].dx = -10
						else:
							players[k].dx = 10








background = glob.glob("sprites/background.png")
background_surface = pygame.image.load(background[0])


t = threading.Thread(target=Server)
t.start()


while 1:
	screen.blit(background_surface, (0,0))

	clock.tick(60)

	for k in players.keys():
		players[k].update()

	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
		elif event.type == KEYDOWN:
			player1.keydown(event.key)

	


	pygame.display.update()
